import {
  SET_SYSTEM,
  SET_NETWORK_TYPE,
  SET_CODE_END_TIME
} from '../types'

export default {
  state: {
    system: null,
    networkType: 'none',
    isIos: false,
    codeEndTime: null
  },
  mutations: {
    [SET_SYSTEM](state, system) {
      state.system = system;
      state.isIos = system.osName.indexOf('ios') !== -1;
    },
    [SET_NETWORK_TYPE](state, networkType) {
      state.networkType = networkType;
    },
    [SET_CODE_END_TIME](state, codeEndTime) {
      state.codeEndTime = codeEndTime;
    }
  },
  actions: {
    saveCodeEndTime({ commit }, time = 60) {
      commit('SET_CODE_END_TIME', new Date() * 1 + time * 1000)
    },
    checkNetwork({ commit }) {
      uni.getNetworkType({
        success: ({ networkType }) => {
          commit('SET_NETWORK_TYPE', networkType);
        }
      });
      uni.onNetworkStatusChange(({ networkType }) => {
        commit('SET_NETWORK_TYPE', networkType);
      });
    },
    getSystem({
      commit
    }) {
      uni.getSystemInfo({
        success: (res) => {
          commit('SET_SYSTEM', {
            ...res
          });
        }
      })
    },
  }
};